﻿
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace HiganUI
{
    // when changing decvie its still on joystick?

    using Ini;
    using HID;
    public partial class inputSetup : Form
    {
        IniFile iniFile;
        int curPlayer = 0;
        bool DEL_PRESS = false;//flag for escape presded
        bool ESC_PRESS = false;//flag for escape presded
        
        string curPage = "SNES";
        HIDControl hidContol = new HIDControl();
        
        public inputSetup()
        {
            InitializeComponent();

            //find HID devices
            List<string> controllers = hidContol.getDevices();
            foreach (string dev in controllers)
            {//populate device combo boxes
                SNES_device_comboBox.Items.Add(dev);
                GBX_device_comboBox.Items.Add(dev);
                NES_device_comboBox.Items.Add(dev);
            }

            //init ini file;
            string p = System.Reflection.Assembly.GetExecutingAssembly().Location;
            p = p.Substring( 0, p.Length - 3 ) + "ini";
            iniFile = new IniFile( p );//first pass creates a file, but in order to write to it I have to do it again. This is only need if the file does not exist.
            iniFile = new IniFile( p );// now I can write to it, CONFUESED!

            loadSettings( 0 );//must be before player combo boxes as player combo boxes triggers events that save. 
            //populate player combo boxes
            SNES_player_comboBox.SelectedIndex = 0;
            NES_player_comboBox.SelectedIndex = 0;
            GBX_player_comboBox.SelectedIndex = 0;
         }

        protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
        {//hey event watcher
            if (keyData == Keys.Delete) DEL_PRESS = true;//for deleteing input values
            if (keyData == Keys.Escape) ESC_PRESS = true;//for canceling input wait
            return base.ProcessCmdKey(ref msg, keyData);
        }
        private void tabControl1_SelectedIndexChanged( object sender, EventArgs e )
        {//changing from tab to tab

            saveSettings( curPlayer );

            //set new page
            TabControl tc = (TabControl)sender;
            TabPage tp = tc.TabPages[tc.SelectedIndex];
            curPage = tp.Name;
            
            //reset player for next tab
            curPlayer = 0;
            SNES_player_comboBox.SelectedIndex=0;
            NES_player_comboBox.SelectedIndex=0;
            GBX_player_comboBox.SelectedIndex=0;

            //set new device
            if ( tabControl1.SelectedTab == tabControl1.TabPages[ "SNES" ] ) SNES_device_comboBox_SelectedIndexChanged( SNES_device_comboBox, EventArgs.Empty );
            if ( tabControl1.SelectedTab == tabControl1.TabPages[ "NES" ] ) NES_device_comboBox_SelectedIndexChanged( NES_device_comboBox, EventArgs.Empty );
            if ( tabControl1.SelectedTab == tabControl1.TabPages[ "GameBoy" ] )GBX_device_comboBox_SelectedIndexChanged( GBX_device_comboBox, EventArgs.Empty );

            loadSettings( curPlayer );

        }

        private void SNES_device_comboBox_SelectedIndexChanged( object sender, EventArgs e )
        {
            if ( SNES_device_comboBox.SelectedItem == null ) { hidContol.deleteDevice(); return; };
            int ID = int.Parse( SNES_device_comboBox.SelectedItem.ToString().Substring( SNES_device_comboBox.SelectedItem.ToString().IndexOf( ":" ) + 1 ) );
            string type = SNES_device_comboBox.SelectedItem.ToString().Substring( 0, SNES_device_comboBox.SelectedItem.ToString().IndexOf( "_" ) );
            hidContol.setDevice( ID, type );
            toggleButtons(curPlayer, "SNES", true);  
        }
        private void NES_device_comboBox_SelectedIndexChanged( object sender, EventArgs e )
        {
            if ( NES_device_comboBox.SelectedItem == null ) { hidContol.deleteDevice(); return; };
            int ID = int.Parse( NES_device_comboBox.SelectedItem.ToString().Substring( NES_device_comboBox.SelectedItem.ToString().IndexOf( ":" ) + 1 ) );
            string type = NES_device_comboBox.SelectedItem.ToString().Substring( 0, NES_device_comboBox.SelectedItem.ToString().IndexOf( "_" ) );
            hidContol.setDevice( ID, type );
            toggleButtons(curPlayer, "NES", true);  
        }
        private void GBX_device_comboBox_SelectedIndexChanged( object sender, EventArgs e )
        {
            if ( GBX_device_comboBox.SelectedItem == null ) { hidContol.deleteDevice(); return; };
            int ID = int.Parse( GBX_device_comboBox.SelectedItem.ToString().Substring( GBX_device_comboBox.SelectedItem.ToString().IndexOf( ":" ) + 1 ) );
            string type = GBX_device_comboBox.SelectedItem.ToString().Substring( 0, GBX_device_comboBox.SelectedItem.ToString().IndexOf( "_" ) );
            hidContol.setDevice( ID, type );
            toggleButtons(curPlayer, "GBX", true);  
        }

        private void SNES_player_comboBox_SelectedIndexChanged( object sender, EventArgs e )
        {
            saveSettings(curPlayer);//first save the player that was just changed
            if (SNES_player_comboBox.SelectedIndex != -1) curPlayer = SNES_player_comboBox.SelectedIndex; //change to selected. 
            loadSettings( curPlayer );//now load the data.
            if (SNES_device_comboBox.SelectedIndex == -1) toggleButtons(curPlayer, "SNES", false); else toggleButtons(curPlayer, "SNES", true);      
        }

        private void NES_player_comboBox_SelectedIndexChanged( object sender, EventArgs e )
        {
            saveSettings( curPlayer );//first save the player that was just changed
            if (NES_player_comboBox.SelectedIndex != -1) curPlayer = NES_player_comboBox.SelectedIndex;//change to selected. 
            loadSettings( curPlayer );//now load the data.
            if (NES_device_comboBox.SelectedIndex == -1) toggleButtons(curPlayer, "NES", false); else toggleButtons(curPlayer, "NES", true);
        }

        private void GBX_player_comboBox_SelectedIndexChanged( object sender, EventArgs e )
        {
            saveSettings( curPlayer );//first save the player that was just changed
            if (GBX_player_comboBox.SelectedIndex != -1) curPlayer = GBX_player_comboBox.SelectedIndex;//change to selected. 
            loadSettings( curPlayer );//now load the data.
            if (GBX_device_comboBox.SelectedIndex == -1) toggleButtons(curPlayer, "GBX", false); else toggleButtons(curPlayer, "GBX", true);
        }

        private void btnOK_Click( object sender, EventArgs e )
        {
            saveSettings( curPlayer ); // save whatever was on the screen.
        }

        private void loadSettings( int player )
        {
            string sys =  curPage;
            
            if ( curPage == "SNES" ) 
            {
                if ( iniFile.IniReadValue(  sys+"_player_" + player, sys + "_device" ) != "" && SNES_device_comboBox.Items.Count > Convert.ToInt16( iniFile.IniReadValue( sys+"_player_" + player, sys + "_device" ) ) ) SNES_device_comboBox.SelectedIndex = Convert.ToInt16( iniFile.IniReadValue( sys+"_player_" + player, sys + "_device" ) ); else  SNES_device_comboBox.SelectedIndex = -1;
                if ( iniFile.IniReadValue(  sys+"_player_" + player, sys + "_type" ) != "" ) SNES_type_comboBox.SelectedIndex = Convert.ToInt16( iniFile.IniReadValue( sys +"_player_" + player, sys +"_type" ) );     else  SNES_type_comboBox.SelectedIndex = -1;
                //buttons states
                if (iniFile.IniReadValue("SNES_player_" + player, "Snes_SL_button") != "") { Snes_SL_button.Tag = Snes_SL_button.Text = iniFile.IniReadValue("SNES_player_" + player, "Snes_SL_button"); Snes_SL_button.Text = Snes_SL_button.Text.Substring(Snes_SL_button.Text.LastIndexOf(":") + 1); } else Snes_SL_button.Text = "...";
                if (iniFile.IniReadValue("SNES_player_" + player, "Snes_SR_button") != "") { Snes_SR_button.Tag = Snes_SR_button.Text = iniFile.IniReadValue("SNES_player_" + player, "Snes_SR_button"); Snes_SR_button.Text = Snes_SR_button.Text.Substring(Snes_SR_button.Text.LastIndexOf(":") + 1); } else Snes_SR_button.Text = "...";
                if (iniFile.IniReadValue("SNES_player_" + player, "Snes_A_button") != "") { Snes_A_button.Tag = Snes_A_button.Text = iniFile.IniReadValue("SNES_player_" + player, "Snes_A_button"); Snes_A_button.Text = Snes_A_button.Text.Substring(Snes_A_button.Text.LastIndexOf(":") + 1); } else Snes_A_button.Text = "...";
                if (iniFile.IniReadValue("SNES_player_" + player, "Snes_B_button") != "") { Snes_B_button.Tag = Snes_B_button.Text = iniFile.IniReadValue("SNES_player_" + player, "Snes_B_button"); Snes_B_button.Text = Snes_B_button.Text.Substring(Snes_B_button.Text.LastIndexOf(":") + 1); } else Snes_B_button.Text = "...";
                if (iniFile.IniReadValue("SNES_player_" + player, "Snes_Y_button") != "") { Snes_Y_button.Tag = Snes_Y_button.Text = iniFile.IniReadValue("SNES_player_" + player, "Snes_Y_button"); Snes_Y_button.Text = Snes_Y_button.Text.Substring(Snes_Y_button.Text.LastIndexOf(":") + 1); } else Snes_Y_button.Text = "...";
                if (iniFile.IniReadValue("SNES_player_" + player, "Snes_X_button") != "") { Snes_X_button.Tag = Snes_X_button.Text = iniFile.IniReadValue("SNES_player_" + player, "Snes_X_button"); Snes_X_button.Text = Snes_X_button.Text.Substring(Snes_X_button.Text.LastIndexOf(":") + 1); }else Snes_X_button.Text = "...";
                if (iniFile.IniReadValue("SNES_player_" + player, "Snes_U_button") != "") { Snes_U_button.Tag = Snes_U_button.Text = iniFile.IniReadValue("SNES_player_" + player, "Snes_U_button"); Snes_U_button.Text = Snes_U_button.Text.Substring(Snes_U_button.Text.LastIndexOf(":") + 1); }else Snes_U_button.Text = "...";
                if (iniFile.IniReadValue("SNES_player_" + player, "Snes_D_button") != "") { Snes_D_button.Tag = Snes_D_button.Text = iniFile.IniReadValue("SNES_player_" + player, "Snes_D_button"); Snes_D_button.Text = Snes_D_button.Text.Substring(Snes_D_button.Text.LastIndexOf(":") + 1); }else Snes_D_button.Text = "...";
                if (iniFile.IniReadValue("SNES_player_" + player, "Snes_L_button") != "") { Snes_L_button.Tag = Snes_L_button.Text = iniFile.IniReadValue("SNES_player_" + player, "Snes_L_button"); Snes_L_button.Text = Snes_L_button.Text.Substring(Snes_L_button.Text.LastIndexOf(":") + 1); }else Snes_L_button.Text = "...";
                if (iniFile.IniReadValue("SNES_player_" + player, "Snes_R_button") != "") { Snes_R_button.Tag = Snes_R_button.Text = iniFile.IniReadValue("SNES_player_" + player, "Snes_R_button"); Snes_R_button.Text = Snes_R_button.Text.Substring(Snes_R_button.Text.LastIndexOf(":") + 1); }else Snes_R_button.Text = "...";
                if (iniFile.IniReadValue("SNES_player_" + player, "Snes_SEL_button") != "") { Snes_SEL_button.Tag = Snes_SEL_button.Text = iniFile.IniReadValue("SNES_player_" + player, "Snes_SEL_button"); Snes_SEL_button.Text = Snes_SEL_button.Text.Substring(Snes_SEL_button.Text.LastIndexOf(":") + 1); }else Snes_SEL_button.Text = "...";
                if (iniFile.IniReadValue("SNES_player_" + player, "Snes_SRT_button") != "") { Snes_SRT_button.Tag = Snes_SRT_button.Text = iniFile.IniReadValue("SNES_player_" + player, "Snes_SRT_button"); Snes_SRT_button.Text = Snes_SRT_button.Text.Substring(Snes_SRT_button.Text.LastIndexOf(":") + 1); }else Snes_SRT_button.Text = "...";
                //mouse button states
                if (iniFile.IniReadValue("SNES_player_" + player, "Snes_M1_button") != "") { Snes_M1_button.Tag = Snes_M1_button.Text = iniFile.IniReadValue("SNES_player_" + player, "Snes_M1_button"); Snes_M1_button.Text = Snes_M1_button.Text.Substring(Snes_M1_button.Text.LastIndexOf(":") + 1); }else Snes_M1_button.Text = "...";
                if (iniFile.IniReadValue("SNES_player_" + player, "Snes_M2_button") != "") { Snes_M2_button.Tag = Snes_M2_button.Text = iniFile.IniReadValue("SNES_player_" + player, "Snes_M2_button"); Snes_M2_button.Text = Snes_M2_button.Text.Substring(Snes_M2_button.Text.LastIndexOf(":") + 1); }else Snes_M2_button.Text = "...";
                //mouse analogs
                if (iniFile.IniReadValue("SNES_player_" + player, "Snes_MX_axis") != "") { Snes_MX_axis.Tag = Snes_MX_axis.Text = iniFile.IniReadValue("SNES_player_" + player, "Snes_MX_axis"); Snes_MX_axis.Text = Snes_MX_axis.Text.Substring(Snes_MX_axis.Text.LastIndexOf(":") + 1); }else Snes_MX_axis.Text = "...";
                if (iniFile.IniReadValue("SNES_player_" + player, "Snes_MY_axis") != "") { Snes_MY_axis.Tag = Snes_MY_axis.Text = iniFile.IniReadValue("SNES_player_" + player, "Snes_MY_axis"); Snes_MY_axis.Text = Snes_MY_axis.Text.Substring(Snes_MY_axis.Text.LastIndexOf(":") + 1); }else Snes_MY_axis.Text = "...";
                
            }

            if ( curPage == "NES" )
            {
                if ( iniFile.IniReadValue( sys + "_player_" + player, sys + "_device" ) != "" && NES_device_comboBox.Items.Count > Convert.ToInt16( iniFile.IniReadValue( sys+"_player_" + player, sys + "_device" ))) NES_device_comboBox.SelectedIndex = Convert.ToInt16( iniFile.IniReadValue( sys + "_player_" + player, sys + "_device" ) ); else NES_device_comboBox.SelectedIndex = -1;
                if ( iniFile.IniReadValue( sys + "_player_" + player, sys + "_type" ) != "" ) NES_type_comboBox.SelectedIndex = Convert.ToInt16( iniFile.IniReadValue( sys +"_player_" + player, sys +"_type" ) ); else NES_type_comboBox.SelectedIndex = -1;
                //buttons states
                if (iniFile.IniReadValue("NES_player_" + player, "NES_A_button") != "") { NES_A_button.Tag = NES_A_button.Text = iniFile.IniReadValue("NES_player_" + player, "NES_A_button"); NES_A_button.Text = NES_A_button.Text.Substring(NES_A_button.Text.LastIndexOf(":") + 1); }
                if (iniFile.IniReadValue("NES_player_" + player, "NES_B_button") != "") { NES_B_button.Tag = NES_B_button.Text = iniFile.IniReadValue("NES_player_" + player, "NES_B_button"); NES_B_button.Text = NES_B_button.Text.Substring(NES_B_button.Text.LastIndexOf(":") + 1); }
                if (iniFile.IniReadValue("NES_player_" + player, "NES_U_button") != "") { NES_U_button.Tag = NES_U_button.Text = iniFile.IniReadValue("NES_player_" + player, "NES_U_button"); NES_U_button.Text = NES_U_button.Text.Substring(NES_U_button.Text.LastIndexOf(":") + 1); }
                if (iniFile.IniReadValue("NES_player_" + player, "NES_D_button") != "") { NES_D_button.Tag = NES_D_button.Text = iniFile.IniReadValue("NES_player_" + player, "NES_D_button"); NES_D_button.Text = NES_D_button.Text.Substring(NES_D_button.Text.LastIndexOf(":") + 1); }
                if (iniFile.IniReadValue("NES_player_" + player, "NES_L_button") != "") { NES_L_button.Tag = NES_L_button.Text = iniFile.IniReadValue("NES_player_" + player, "NES_L_button"); NES_L_button.Text = NES_L_button.Text.Substring(NES_L_button.Text.LastIndexOf(":") + 1); }
                if (iniFile.IniReadValue("NES_player_" + player, "NES_R_button") != "") { NES_R_button.Tag = NES_R_button.Text = iniFile.IniReadValue("NES_player_" + player, "NES_R_button"); NES_R_button.Text = NES_R_button.Text.Substring(NES_R_button.Text.LastIndexOf(":") + 1); }
                if (iniFile.IniReadValue("NES_player_" + player, "NES_SEL_button") != "") { NES_SEL_button.Tag = NES_SEL_button.Text = iniFile.IniReadValue("NES_player_" + player, "NES_SEL_button"); NES_SEL_button.Text = NES_SEL_button.Text.Substring(NES_SEL_button.Text.LastIndexOf(":") + 1); }
                if (iniFile.IniReadValue("NES_player_" + player, "NES_SRT_button") != "") { NES_SRT_button.Tag = NES_SRT_button.Text = iniFile.IniReadValue("NES_player_" + player, "NES_SRT_button"); NES_SRT_button.Text = NES_SRT_button.Text.Substring(NES_SRT_button.Text.LastIndexOf(":") + 1); }
                //arkanoid 
                if (iniFile.IniReadValue("NES_player_" + player, "NES_ARKX_vaus") != "") { NES_ARKX_vaus.Tag = NES_ARKX_vaus.Text = iniFile.IniReadValue("NES_player_" + player, "NES_ARKX_vaus"); NES_ARKX_vaus.Text = NES_ARKX_vaus.Text.Substring(NES_ARKX_vaus.Text.LastIndexOf(":") + 1); }
                if (iniFile.IniReadValue("NES_player_" + player, "NES_ARKA_button") != "") { NES_ARKA_button.Tag = NES_ARKA_button.Text = iniFile.IniReadValue("NES_player_" + player, "NES_ARKA_button"); NES_ARKA_button.Text = NES_ARKA_button.Text.Substring(NES_ARKA_button.Text.LastIndexOf(":") + 1); }
                //gun
                if (iniFile.IniReadValue("NES_player_" + player, "NES_GUN_button") != "") { NES_GUN_button.Tag = NES_GUN_button.Text = iniFile.IniReadValue("NES_player_" + player, "NES_GUN_button"); NES_GUN_button.Text = NES_GUN_button.Text.Substring(NES_GUN_button.Text.LastIndexOf(":") + 1); }
                if (iniFile.IniReadValue("NES_player_" + player, "NES_GUNX_scope") != "") { NES_GUNY_scope.Tag = NES_GUNX_scope.Text = iniFile.IniReadValue("NES_player_" + player, "NES_GUNX_scope"); NES_GUNX_scope.Text = NES_GUNX_scope.Text.Substring(NES_GUNX_scope.Text.LastIndexOf(":") + 1); }
                if (iniFile.IniReadValue("NES_player_" + player, "NES_GUNY_scope") != "") { NES_GUNY_scope.Tag = NES_GUNY_scope.Text = iniFile.IniReadValue("NES_player_" + player, "NES_GUNY_scope"); NES_GUNY_scope.Text = NES_GUNY_scope.Text.Substring(NES_GUNY_scope.Text.LastIndexOf(":") + 1); }
            }
            if ( curPage == "GameBoy" )
            {
                if (iniFile.IniReadValue(sys + "_player_" + player, sys + "_device") != "" && GBX_device_comboBox.Items.Count > Convert.ToInt16(iniFile.IniReadValue(sys + "_player_" + player, sys + "_device"))) GBX_device_comboBox.SelectedIndex = Convert.ToInt16(iniFile.IniReadValue(sys + "_player_" + player, sys + "_device")); else GBX_device_comboBox.SelectedIndex = -1;
                //
                if (iniFile.IniReadValue("GBX_player_" + player, "GBX_A_button") != "") { GBX_A_button.Tag = GBX_A_button.Text = iniFile.IniReadValue("GBX_player_" + player, "GBX_A_button"); GBX_A_button.Text = GBX_A_button.Text.Substring(GBX_A_button.Text.LastIndexOf(":") + 1); }
                if (iniFile.IniReadValue("GBX_player_" + player, "GBX_B_button") != "") { GBX_B_button.Tag = GBX_B_button.Text = iniFile.IniReadValue("GBX_player_" + player, "GBX_B_button"); GBX_B_button.Text = GBX_B_button.Text.Substring(GBX_B_button.Text.LastIndexOf(":") + 1); }
                if (iniFile.IniReadValue("GBX_player_" + player, "GBX_U_button") != "") { GBX_U_button.Tag = GBX_U_button.Text = iniFile.IniReadValue("GBX_player_" + player, "GBX_U_button"); GBX_U_button.Text = GBX_U_button.Text.Substring(GBX_U_button.Text.LastIndexOf(":") + 1); }
                if (iniFile.IniReadValue("GBX_player_" + player, "GBX_D_button") != "") { GBX_D_button.Tag = GBX_D_button.Text = iniFile.IniReadValue("GBX_player_" + player, "GBX_D_button"); GBX_D_button.Text = GBX_D_button.Text.Substring(GBX_D_button.Text.LastIndexOf(":") + 1); }
                if (iniFile.IniReadValue("GBX_player_" + player, "GBX_L_button") != "") { GBX_L_button.Tag = GBX_L_button.Text = iniFile.IniReadValue("GBX_player_" + player, "GBX_L_button"); GBX_L_button.Text = GBX_L_button.Text.Substring(GBX_L_button.Text.LastIndexOf(":") + 1); }
                if (iniFile.IniReadValue("GBX_player_" + player, "GBX_R_button") != "") { GBX_R_button.Tag = GBX_R_button.Text = iniFile.IniReadValue("GBX_player_" + player, "GBX_R_button"); GBX_R_button.Text = GBX_R_button.Text.Substring(GBX_R_button.Text.LastIndexOf(":") + 1); }
                if (iniFile.IniReadValue("GBX_player_" + player, "GBX_SL_button") != "") { GBX_SL_button.Tag = GBX_SL_button.Text = iniFile.IniReadValue("GBX_player_" + player, "GBX_SL_button"); GBX_SL_button.Text = GBX_SL_button.Text.Substring(GBX_SL_button.Text.LastIndexOf(":") + 1); }
                if (iniFile.IniReadValue("GBX_player_" + player, "GBX_SR_button") != "") { GBX_SR_button.Tag = GBX_SR_button.Text = iniFile.IniReadValue("GBX_player_" + player, "GBX_SR_button"); GBX_SR_button.Text = GBX_SR_button.Text.Substring(GBX_SR_button.Text.LastIndexOf(":") + 1); }
                if (iniFile.IniReadValue("GBX_player_" + player, "GBX_SEL_button") != "") { GBX_SEL_button.Tag = GBX_SEL_button.Text = iniFile.IniReadValue("GBX_player_" + player, "GBX_SEL_button"); GBX_SEL_button.Text = GBX_SEL_button.Text.Substring(GBX_SEL_button.Text.LastIndexOf(":") + 1); }
                if (iniFile.IniReadValue("GBX_player_" + player, "GBX_SRT_button") != "") { GBX_SRT_button.Tag = GBX_SRT_button.Text = iniFile.IniReadValue("GBX_player_" + player, "GBX_SRT_button"); GBX_SRT_button.Text = GBX_SRT_button.Text.Substring(GBX_SRT_button.Text.LastIndexOf(":") + 1); }
            }
        }

        private void saveSettings( int player )
        {
            if ( curPage == "SNES" )
            {
                if ( SNES_device_comboBox.SelectedIndex != -1 ) iniFile.IniWriteValue( "SNES_player_" + player, "SNES_device", SNES_device_comboBox.SelectedIndex.ToString() );
                if ( SNES_type_comboBox.SelectedIndex != -1 )   iniFile.IniWriteValue( "SNES_player_" + player, "SNES_type", SNES_type_comboBox.SelectedIndex.ToString() );
                //buttons states
                if (Snes_SL_button.Tag != null) iniFile.IniWriteValue("SNES_player_" + player, "Snes_SL_button", Snes_SL_button.Tag.ToString() ); else iniFile.IniWriteValue("SNES_player_" + player, "Snes_SL_button", "");
                if (Snes_SR_button.Tag != null) iniFile.IniWriteValue("SNES_player_" + player, "Snes_SR_button", Snes_SR_button.Tag.ToString()); else iniFile.IniWriteValue("SNES_player_" + player, "Snes_SR_button", "");
                if (Snes_A_button.Tag != null) iniFile.IniWriteValue("SNES_player_" + player, "Snes_A_button", Snes_A_button.Tag.ToString()); else iniFile.IniWriteValue("SNES_player_" + player, "Snes_A_button", "");
                if (Snes_B_button.Tag != null) iniFile.IniWriteValue("SNES_player_" + player, "Snes_B_button", Snes_B_button.Tag.ToString()); else iniFile.IniWriteValue("SNES_player_" + player, "Snes_B_button", "");
                if (Snes_Y_button.Tag != null) iniFile.IniWriteValue("SNES_player_" + player, "Snes_Y_button", Snes_Y_button.Tag.ToString()); else iniFile.IniWriteValue("SNES_player_" + player, "Snes_Y_button", "");
                if (Snes_X_button.Tag != null) iniFile.IniWriteValue("SNES_player_" + player, "Snes_X_button", Snes_X_button.Tag.ToString()); else iniFile.IniWriteValue("SNES_player_" + player, "Snes_X_button", "");
                if (Snes_U_button.Tag != null) iniFile.IniWriteValue("SNES_player_" + player, "Snes_U_button", Snes_U_button.Tag.ToString()); else iniFile.IniWriteValue("SNES_player_" + player, "Snes_U_button", "");
                if (Snes_D_button.Tag != null) iniFile.IniWriteValue("SNES_player_" + player, "Snes_D_button", Snes_D_button.Tag.ToString()); else iniFile.IniWriteValue("SNES_player_" + player, "Snes_D_button", "");
                if (Snes_L_button.Tag != null) iniFile.IniWriteValue("SNES_player_" + player, "Snes_L_button", Snes_L_button.Tag.ToString()); else iniFile.IniWriteValue("SNES_player_" + player, "Snes_L_button", "");
                if (Snes_R_button.Tag != null) iniFile.IniWriteValue("SNES_player_" + player, "Snes_R_button", Snes_R_button.Tag.ToString()); else iniFile.IniWriteValue("SNES_player_" + player, "Snes_R_button", "");
                if (Snes_SEL_button.Tag != null) iniFile.IniWriteValue("SNES_player_" + player, "Snes_SEL_button", Snes_SEL_button.Tag.ToString()); else iniFile.IniWriteValue("SNES_player_" + player, "Snes_SEL_button", "");
                if (Snes_SRT_button.Tag != null) iniFile.IniWriteValue("SNES_player_" + player, "Snes_SRT_button", Snes_SRT_button.Tag.ToString()); else iniFile.IniWriteValue("SNES_player_" + player, "Snes_SRT_button", "");
                //mouse button states
                if ((Snes_M1_button.Tag != null)) iniFile.IniWriteValue("SNES_player_" + player, "Snes_M1_button", Snes_M1_button.Tag.ToString()); else iniFile.IniWriteValue("SNES_player_" + player, "Snes_M1_button", "");
                if ((Snes_M2_button.Tag != null)) iniFile.IniWriteValue("SNES_player_" + player, "Snes_M2_button", Snes_M2_button.Tag.ToString()); else iniFile.IniWriteValue("SNES_player_" + player, "Snes_M2_button", "");
                //mouse analogs
                if ((Snes_MX_axis.Tag != null)) iniFile.IniWriteValue("SNES_player_" + player, "Snes_MX_axis", Snes_MX_axis.Tag.ToString()); else iniFile.IniWriteValue("SNES_player_" + player, "Snes_MX_axis", "");
                if ((Snes_MY_axis.Tag != null)) iniFile.IniWriteValue("SNES_player_" + player, "Snes_MY_axis", Snes_MY_axis.Tag.ToString()); else iniFile.IniWriteValue("SNES_player_" + player, "Snes_MY_axis", "");
                

            }

            if ( curPage == "NES" ) 
            {
                if ( NES_device_comboBox.SelectedIndex != -1 ) iniFile.IniWriteValue( "NES_player_" + player, "NES_device", NES_device_comboBox.SelectedIndex.ToString() );
                if ( NES_type_comboBox.SelectedIndex != -1 ) iniFile.IniWriteValue( "NES_player_" + player, "NES_type", NES_type_comboBox.SelectedIndex.ToString() );
                //buttons states
                if (NES_A_button.Tag != null) iniFile.IniWriteValue("NES_player_" + player, "NES_A_button", NES_A_button.Tag.ToString()); else iniFile.IniWriteValue("NES_player_" + player, "NES_A_button", "");
                if (NES_B_button.Tag != null) iniFile.IniWriteValue("NES_player_" + player, "NES_B_button", NES_B_button.Tag.ToString()); else iniFile.IniWriteValue("NES_player_" + player, "NES_B_button", "");
                if (NES_U_button.Tag != null) iniFile.IniWriteValue("NES_player_" + player, "NES_U_button", NES_U_button.Tag.ToString()); else iniFile.IniWriteValue("NES_player_" + player, "NES_U_button", "");
                if (NES_D_button.Tag != null) iniFile.IniWriteValue("NES_player_" + player, "NES_D_button", NES_D_button.Tag.ToString()); else iniFile.IniWriteValue("NES_player_" + player, "NES_D_button", "");
                if (NES_L_button.Tag != null) iniFile.IniWriteValue("NES_player_" + player, "NES_L_button", NES_L_button.Tag.ToString()); else iniFile.IniWriteValue("NES_player_" + player, "NES_L_button", "");
                if (NES_R_button.Tag != null) iniFile.IniWriteValue("NES_player_" + player, "NES_R_button", NES_R_button.Tag.ToString()); else iniFile.IniWriteValue("NES_player_" + player, "NES_R_button", "");
                if (NES_SEL_button.Tag != null) iniFile.IniWriteValue("NES_player_" + player, "NES_SEL_button", NES_SEL_button.Tag.ToString()); else iniFile.IniWriteValue("NES_player_" + player, "NES_SEL_button", "");
                if (NES_SRT_button.Tag != null) iniFile.IniWriteValue("NES_player_" + player, "NES_SRT_button", NES_SRT_button.Tag.ToString()); else iniFile.IniWriteValue("NES_player_" + player, "NES_SRT_button", "");
                //arkanoid 
                if (NES_ARKX_vaus.Tag != null) iniFile.IniWriteValue("NES_player_" + player, "NES_ARKX_vaus", NES_ARKX_vaus.Tag.ToString()); else iniFile.IniWriteValue("NES_player_" + player, "NES_ARKX_vaus", "");
                if (NES_ARKA_button.Tag != null) iniFile.IniWriteValue("NES_player_" + player, "NES_ARKA_button", NES_ARKA_button.Tag.ToString()); else iniFile.IniWriteValue("NES_player_" + player, "NES_ARXA_button", "");
                //gun
                if (NES_GUN_button.Tag != null) iniFile.IniWriteValue("NES_player_" + player, "NES_GUN_button", NES_GUN_button.Tag.ToString()); else iniFile.IniWriteValue("NES_player_" + player, "NES_GUN_button", "");
                if (NES_GUNX_scope.Tag != null) iniFile.IniWriteValue("NES_player_" + player, "NES_GUNX_scope", NES_GUNX_scope.Tag.ToString()); else iniFile.IniWriteValue("NES_player_" + player, "NES_GUNX_scope", "");
                if (NES_GUNY_scope.Tag != null) iniFile.IniWriteValue("NES_player_" + player, "NES_GUNY_scope", NES_GUNY_scope.Tag.ToString()); else iniFile.IniWriteValue("NES_player_" + player, "NES_GUNY_scope", "");
            }

            if ( curPage == "GameBoy" )
            {
                if ( GBX_device_comboBox.SelectedIndex != -1 ) iniFile.IniWriteValue( "GBX_player_" + player, "GBX_device", GBX_device_comboBox.SelectedIndex.ToString() );
                //buttons states
                if (GBX_A_button.Tag != null) iniFile.IniWriteValue("GBX_player_" + player, "GBX_A_button", GBX_A_button.Tag.ToString()); else iniFile.IniWriteValue("GBX_player_" + player, "GBX_A_button", "");
                if (GBX_B_button.Tag != null) iniFile.IniWriteValue("GBX_player_" + player, "GBX_B_button", GBX_B_button.Tag.ToString()); else iniFile.IniWriteValue("GBX_player_" + player, "GBX_B_button", "");
                if (GBX_U_button.Tag != null) iniFile.IniWriteValue("GBX_player_" + player, "GBX_U_button", GBX_U_button.Tag.ToString()); else iniFile.IniWriteValue("GBX_player_" + player, "GBX_U_button", "");
                if (GBX_D_button.Tag != null) iniFile.IniWriteValue("GBX_player_" + player, "GBX_D_button", GBX_D_button.Tag.ToString()); else iniFile.IniWriteValue("GBX_player_" + player, "GBX_D_button", "");
                if (GBX_L_button.Tag != null) iniFile.IniWriteValue("GBX_player_" + player, "GBX_L_button", GBX_L_button.Tag.ToString()); else iniFile.IniWriteValue("GBX_player_" + player, "GBX_L_button", "");
                if (GBX_R_button.Tag != null) iniFile.IniWriteValue("GBX_player_" + player, "GBX_R_button", GBX_R_button.Tag.ToString()); else iniFile.IniWriteValue("GBX_player_" + player, "GBX_R_button", "");
                if (GBX_SL_button.Tag != null) iniFile.IniWriteValue("GBX_player_" + player, "GBX_SL_button", GBX_SL_button.Tag.ToString()); else iniFile.IniWriteValue("GBX_player_" + player, "GBX_SL_button", "");
                if (GBX_SR_button.Tag != null) iniFile.IniWriteValue("GBX_player_" + player, "GBX_SR_button", GBX_SR_button.Tag.ToString()); else iniFile.IniWriteValue("GBX_player_" + player, "GBX_SR_button", "");
                if (GBX_SEL_button.Tag != null) iniFile.IniWriteValue("GBX_player_" + player, "GBX_SEL_button", GBX_SEL_button.Tag.ToString()); else iniFile.IniWriteValue("GBX_player_" + player, "GBX_SEL_button", "");
                if (GBX_SRT_button.Tag != null) iniFile.IniWriteValue("GBX_player_" + player, "GBX_SRT_button", GBX_SRT_button.Tag.ToString()); else iniFile.IniWriteValue("GBX_player_" + player, "GBX_SRT_button", "");
            }


        }
        private void toggleButtons(int p, string sys, bool state)
        {
            if (sys == "SNES")
            {
                Snes_SL_button.Enabled = state;
                Snes_SR_button.Enabled = state;
                Snes_A_button.Enabled = state;
                Snes_B_button.Enabled = state;
                Snes_X_button.Enabled = state;
                Snes_Y_button.Enabled = state;
                Snes_SEL_button.Enabled = state;
                Snes_SRT_button.Enabled = state;
                Snes_U_button.Enabled = state;
                Snes_D_button.Enabled = state;
                Snes_L_button.Enabled = state;
                Snes_R_button.Enabled = state;
                Snes_MX_axis.Enabled = state;
                Snes_MY_axis.Enabled = state;
                Snes_M1_button.Enabled = state;
                Snes_M2_button.Enabled = state;
            }
            if (sys == "NES")
            {
                NES_A_button.Enabled = state;
                NES_B_button.Enabled = state;
                NES_SEL_button.Enabled = state;
                NES_SRT_button.Enabled = state;
                NES_U_button.Enabled = state;
                NES_D_button.Enabled = state;
                NES_L_button.Enabled = state;
                NES_R_button.Enabled = state;
                NES_ARKA_button.Enabled = state;
                NES_ARKX_vaus.Enabled = state;
                NES_GUN_button.Enabled = state;
                NES_GUNX_scope.Enabled = state;
                NES_GUNY_scope.Enabled = state;
            }
            if (sys == "GBX")
            {
                GBX_A_button.Enabled = state;
                GBX_B_button.Enabled = state;
                GBX_SEL_button.Enabled = state;
                GBX_SRT_button.Enabled = state;
                GBX_SR_button.Enabled = state;
                GBX_SL_button.Enabled = state;
                GBX_U_button.Enabled = state;
                GBX_D_button.Enabled = state;
                GBX_L_button.Enabled = state;
                GBX_R_button.Enabled = state;
            }
        }
        private void inputSetup_Load( object sender, EventArgs e )
        {
        }

        private void Snes_SL_button_Click(object sender, EventArgs e) { getButtonImput((Button)sender); }
        private void Snes_SR_button_Click(object sender, EventArgs e) { getButtonImput((Button)sender); }
        private void Snes_A_button_Click(object sender, EventArgs e) { getButtonImput((Button)sender); }
        private void Snes_B_button_Click(object sender, EventArgs e) { getButtonImput((Button)sender); }
        private void Snes_X_button_Click(object sender, EventArgs e) { getButtonImput((Button)sender); }
        private void Snes_Y_button_Click(object sender, EventArgs e) { getButtonImput((Button)sender); }
        private void Snes_SEL_button_Click(object sender, EventArgs e) { getButtonImput((Button)sender); }
        private void Snes_SRT_button_Click(object sender, EventArgs e) { getButtonImput((Button)sender); }
        private void Snes_U_button_Click(object sender, EventArgs e) { getButtonImput((Button)sender); }
        private void Snes_D_button_Click(object sender, EventArgs e) { getButtonImput((Button)sender); }
        private void Snes_L_button_Click(object sender, EventArgs e) { getButtonImput((Button)sender); }
        private void Snes_R_button_Click(object sender, EventArgs e) { getButtonImput((Button)sender); }
        private void Snes_MX_axis_Click(object sender, EventArgs e) { getButtonImput((Button)sender); }
        private void Snes_MY_axis_Click(object sender, EventArgs e) { getButtonImput((Button)sender); }
        private void Snes_M1_button_Click(object sender, EventArgs e) { getButtonImput((Button)sender); }
        private void Snes_M2_button_Click(object sender, EventArgs e) { getButtonImput((Button)sender); }
        private void NES_A_button_Click(object sender, EventArgs e) { getButtonImput((Button)sender); }
        private void NES_B_button_Click(object sender, EventArgs e) { getButtonImput((Button)sender); }
        private void NES_SEL_button_Click(object sender, EventArgs e) { getButtonImput((Button)sender); }
        private void NES_SRT_button_Click(object sender, EventArgs e) { getButtonImput((Button)sender); }
        private void NES_U_button_Click(object sender, EventArgs e) { getButtonImput((Button)sender); }
        private void NES_D_button_Click(object sender, EventArgs e) { getButtonImput((Button)sender); }
        private void NES_L_button_Click(object sender, EventArgs e) { getButtonImput((Button)sender); }
        private void NES_R_button_Click(object sender, EventArgs e) { getButtonImput((Button)sender); }
        private void NES_ARKA_button_Click(object sender, EventArgs e) { getButtonImput((Button)sender); }
        private void NES_ARKX_vaus_Click(object sender, EventArgs e) { getButtonImput((Button)sender); }
        private void NES_GUN_button_Click(object sender, EventArgs e) { getButtonImput((Button)sender); }
        private void NES_GUNX_scope_Click(object sender, EventArgs e) { getButtonImput((Button)sender); }
        private void NES_GUNY_scope_Click(object sender, EventArgs e) { getButtonImput((Button)sender); }
        private void GBX_A_button_Click(object sender, EventArgs e) { getButtonImput((Button)sender); }
        private void GBX_B_button_Click(object sender, EventArgs e) { getButtonImput((Button)sender); }
        private void GBX_SEL_button_Click(object sender, EventArgs e) { getButtonImput((Button)sender); }
        private void GBX_SRT_button_Click(object sender, EventArgs e) { getButtonImput((Button)sender); }
        private void GBX_SR_button_Click(object sender, EventArgs e) { getButtonImput((Button)sender); }
        private void GBX_SL_button_Click(object sender, EventArgs e) { getButtonImput((Button)sender); }
        private void GBX_U_button_Click(object sender, EventArgs e) { getButtonImput((Button)sender); }
        private void GBX_D_button_Click(object sender, EventArgs e) { getButtonImput((Button)sender); }
        private void GBX_L_button_Click(object sender, EventArgs e) { getButtonImput((Button)sender); }
        private void GBX_R_button_Click(object sender, EventArgs e) { getButtonImput((Button)sender); }

        private void getButtonImput( Button button )
        {
            button.Enabled = false; // prevents button from accepting input
            int count = 2000000;
            string oldText = button.Text;
            button.Text = "Waiting...";
            button.Update();
            int ID=-1;
            string type="-1";
            //find system
            if (button.Name.Substring(0,button.Name.IndexOf("_")) == "Snes")
            {
                ID = int.Parse(SNES_device_comboBox.SelectedItem.ToString().Substring(SNES_device_comboBox.SelectedItem.ToString().IndexOf(":") + 1));
                type = SNES_device_comboBox.SelectedItem.ToString().Substring(0, SNES_device_comboBox.SelectedItem.ToString().IndexOf("_"));
            }
            if (button.Name.Substring(0, button.Name.IndexOf("_")) == "NES")
            {
                ID = int.Parse(NES_device_comboBox.SelectedItem.ToString().Substring(NES_device_comboBox.SelectedItem.ToString().IndexOf(":") + 1));
                type = NES_device_comboBox.SelectedItem.ToString().Substring(0, NES_device_comboBox.SelectedItem.ToString().IndexOf("_"));
            }
            if (button.Name.Substring(0, button.Name.IndexOf("_")) == "GBX")
            {
                ID = int.Parse(GBX_device_comboBox.SelectedItem.ToString().Substring(GBX_device_comboBox.SelectedItem.ToString().IndexOf(":") + 1));
                type = GBX_device_comboBox.SelectedItem.ToString().Substring(0, GBX_device_comboBox.SelectedItem.ToString().IndexOf("_"));
            }

            while (count > 1)//check for button
            {
                Application.DoEvents();
                if (DEL_PRESS) { hidContol.setDeviceLock( false); button.Tag = button.Text = "..."; DEL_PRESS = false; button.Enabled = true; return; }// this will delete the input
                if ( ESC_PRESS )
                {
                    hidContol.setDeviceLock( false );
                    ESC_PRESS = false;
                    button.Enabled = true;
                    button.Text = oldText;
                    return;
                }
                count--;

                string returnText ="";
                string returnTextB = hidContol.getDeviceButton( ID, type );// id:type:button_friendlyName
                string returnTextA = hidContol.getDeviceAnalog( ID, type );// id:type:button_friendlyName
                if ( returnTextB == "Waiting..." )  returnText = returnTextA; else returnText = returnTextB;

                if (returnText != "Waiting...") 
                { //button pressed.
                    button.Tag = returnText;//info
                    button.Text = returnText.Substring(returnText.LastIndexOf(":") + 1);//friendly name
                    button.Update();
                    button.Enabled = true; // re enable.
                    hidContol.setDeviceLock( false);
                    return; 
                }
            }
            hidContol.setDeviceLock( false);
            button.Enabled = true;//reenable button inputs
            button.Text = oldText;//gave up

        }
    }
}
